<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Requests\Api\ApiRequest as Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Interfaces\AddressService;

use App\Exceptions\Api\ApiDoneErrorException;
use App\Exceptions\Api\ApiInputErrorException;
use App\Exceptions\Api\ApiRsaEecryptErrorException;
use App\Exceptions\Api\ApiUserNotLoginErrorException;
use App\Exceptions\Api\ApiValidateErrorException;
use App\Exceptions\Interfaces\AddressServiceException;

class AddressController extends Controller
{

    /**
     * @api {post} /address/get_user_address 获取用户收货地址
     * @apiName Address/get_user_address
     * @apiGroup Address
     * @apiDescription 获取用户收货地址.
     *
     * @apiSuccess {Object[]}  address      用户售后地址
     * @apiSuccess {Number}  address_id     收货地址id
     * @apiSuccess {Number}  user_id        用户id
     * @apiSuccess {String}  id_card        用户证件号（一般是身份证号）
     * @apiSuccess {String}  consignee      收货人姓名
     * @apiSuccess {String}  country        国家
     * @apiSuccess {String}  position_ids   省市区id集
     * @apiSuccess {String}  province       省份
     * @apiSuccess {String}  city           城市
     * @apiSuccess {String}  district       街道
     * @apiSuccess {String}  address        地址
     * @apiSuccess {String}  mobile         收货人电话
     * @apiSuccess {Number}  is_default     是否是默认地址，0：非默认地址；1：默认地址
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //用户收货地址获取成功
     * [
     *   {
     *     "address_id": 9,
     *     "user_id": 59,
     *     "id_card": "411024198903052223",
     *     "consignee": "李安",
     *     "country": "中国",
     *     "position_ids": "110000,110100,110107",
     *     "province": "北京市",
     *     "city": "北京市",
     *     "district": "石景山区",
     *     "address": "西城路22号",
     *     "mobile": "17737132820",
     *     "is_default": 1
     *   },
     *   {
     *     "address_id": 5,
     *     "user_id": 59,
     *     "id_card": "411521199212208346",
     *     "consignee": "王鹏",
     *     "country": "中国",
     *     "position_ids": "410000,410500,410522",
     *     "province": "河南省",
     *     "city": "安阳市",
     *     "district": "安阳县",
     *     "address": "中原中路82号",
     *     "mobile": "15890121887",
     *     "is_default": 0
     *   }
     * ]
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 403
     * //用户收货地址获取失败
     * {
     *   "message": "用户id不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 202
     * //用户收货地址获取失败
     * {
     *   "message": "没有该用户的收货地址信息"
     * }
     */
    public function getUserAddress(Request $request, AddressService $addressService)
    {

        $user_id = $request->apiUser->get()->id;

        if (empty($user_id)) {

            throw new ApiUserNotLoginErrorException("用户id不能为空");
        }//end if()

        $address = $addressService->getUserAddress($user_id);
        if (empty($address)) {

            throw new ApiDoneErrorException("没有该用户的收货地址信息");
        }//end if()

        return response()->json($address);

    }//end func getUserAddress


    /**
     * @api {post} /address/get_address 根据收货地址id获取用户收货地址
     * @apiName Address/get_address
     * @apiGroup Address
     * @apiDescription 根据收货地址id获取用户收货地址.
     *
     * @apiParam {Number} address_id       收货地址id.
     *
     * @apiSuccess {Object[]}  address      用户售后地址
     * @apiSuccess {Number}  address_id     收货地址id
     * @apiSuccess {Number}  user_id        用户id
     * @apiSuccess {String}  id_card        用户证件号（一般是身份证号）
     * @apiSuccess {String}  consignee      收货人姓名
     * @apiSuccess {String}  country        国家
     * @apiSuccess {String}  position_ids   省市区id集
     * @apiSuccess {String}  province       省份
     * @apiSuccess {String}  city           城市
     * @apiSuccess {String}  district       街道
     * @apiSuccess {String}  address        地址
     * @apiSuccess {String}  mobile         收货人电话
     * @apiSuccess {Number}  is_default     是否是默认地址，0：非默认地址；1：默认地址
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //用户收货地址获取成功
     * {
     *   "address_id": 9,
     *   "user_id": 59,
     *   "id_card": "411024198903052223",
     *   "consignee": "李安",
     *   "country": "中国",
     *   "position_ids": "110000,110100,110107",
     *   "province": "北京市",
     *   "city": "北京市",
     *   "district": "石景山区",
     *   "address": "西城路22号",
     *   "mobile": "17737132820",
     *   "is_default": 1
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //用户收货地址获取失败
     * {
     *   "message": "收货地址id不能为空"
     * }
     * @apiErrorExample Error-Response 0:
     * HTTP/1.1 403
     * //用户收货地址获取失败
     * {
     *   "message": "用户id不能为空"
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 202
     * //用户收货地址获取失败
     * {
     *   "message": "该收货地址不属于当前用户"
     * }
     */
    public function getAddress(Request $request, AddressService $addressService){


        $address_id = $request->address_id;
        $user_id = $request->apiUser->get()->id;

        if (empty($address_id)) {

            throw new ApiInputErrorException("收货地址id不能为空");
        }//end if()

        if (empty($user_id)) {

            throw new ApiUserNotLoginErrorException("用户id不能为空");
        }//end if()

        $address = $addressService->getAddress($address_id);
        if ($address) {

            $user_id2 = $address->user_id;

            if($user_id != $user_id2){
                throw new ApiUserNotLoginErrorException("该收货地址不属于当前用户");
            }//end if()

            return response()->json($address);
        }//end if()

    }//end  func getAddress

    /**
     * @api {post} /address/add_address 增加收货地址
     * @apiName Address/add_address
     * @apiGroup Address
     * @apiDescription 增加收货地址.
     *
     * @apiParam {String[]} addressArr       地址信息数组，如：addressArr = array('country'=>'中国','position_ids'=>'110000,110100,110107','province'=>'北京市','city'=>'北京市','district'=>'石景山区','address'=>'奋斗路1号','consignee'=>'小明','id_card'=>'411025199001028888','mobile'=>'15136168236','is_default'=>'0',).
     * @apiParam {String} country       国家.
     * @apiParam {String} position_ids  省市区id集.
     * @apiParam {String} province      省份（河南省，北京市）.
     * @apiParam {String} city          城市.
     * @apiParam {String} district      区.
     * @apiParam {String} address       详细地址.
     * @apiParam {String} consignee     收货人姓名.
     * @apiParam {String} id_card       用户证件号（一般是身份证号）.
     * @apiParam {String} mobile        电话号码.
     * @apiParam {Number} is_default    是否是默认地址，0：非默认地址；1：默认地址.
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //用户收货地址添加成功
     * {
     *   "address_id": 9
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 403
     * //用户收货地址添加失败
     * {
     *   "message": "用户id不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //用户收货地址添加失败
     * {
     *   "message": "地址信息数组不能为空"
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 400
     * //用户收货地址添加失败
     * {
     *   "message": "收货地址最多只能添加20个"
     * }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 400
     * //用户收货地址添加失败
     * {
     *   "message": "收货人参数不能为空"
     * }
     * @apiErrorExample Error-Response 05:
     * HTTP/1.1 400
     * //用户收货地址添加失败
     * {
     *   "message": "收货人身份证id参数不能为空"
     * }
     * @apiErrorExample Error-Response 06:
     * HTTP/1.1 400
     * //用户收货地址添加失败
     * {
     *   "message": "收货人参数不能为空"
     * }
     * @apiErrorExample Error-Response 07:
     * HTTP/1.1 400
     * //用户收货地址添加失败
     * {
     *   "message": "收货地址省份参数不能为空"
     * }
     * @apiErrorExample Error-Response 08:
     * HTTP/1.1 400
     * //用户收货地址添加失败
     * {
     *   "message": "收货地址城市参数不能为空"
     * }
     * @apiErrorExample Error-Response 09:
     * HTTP/1.1 400
     * //用户收货地址添加失败
     * {
     *   "message": "收货地址区域参数不能为空"
     * }
     * @apiErrorExample Error-Response 10:
     * HTTP/1.1 400
     * //用户收货地址添加失败
     * {
     *   "message": "收货地址街道参数不能为空"
     * }
     * @apiErrorExample Error-Response 11:
     * HTTP/1.1 400
     * //用户收货地址添加失败
     * {
     *   "message": "收货人手机号码参数不能为空"
     * }
     */
    public function addAddress(Request $request, AddressService $addressService)
    {
        $addressArr = [];
        $user_id = $request->apiUser->get()->id;
        if (empty($user_id)) {

            throw new ApiUserNotLoginErrorException("用户id不能为空");
        }//end if()

        $addressArr = $request->addressArr;
        if(empty($addressArr)){

            throw new ApiInputErrorException("地址信息数组不能为空");
        }//end if()

        try {

            $address_id = $addressService->addAddress($user_id, $addressArr);

        } catch (AddressServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());
        }

        return response()->json(['address_id' => $address_id]);

    }//end func addAddress


    /**
     * @api {post} /address/remove_address 删除收货地址
     * @apiName Address/remove_address
     * @apiGroup Address
     * @apiDescription 删除收货地址.
     *
     * @apiParam {String} address_id  地址id.
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //删除收货地址成功
     * {
     *   "result": "true"
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 400
     * //用户收货地址添加失败
     * {
     *   "message": "用户id不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //用户收货地址添加失败
     * {
     *   "message": "地址id不能为空"
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 500
     * //用户收货地址添加失败
     * {
     *   "message": "要删除的收货地址id不能为空"
     * }
     *
     */
    public function removeAddress(Request $request, AddressService $addressService)
    {

        $user_id = $request->apiUser->get()->id;
        if (empty($user_id)) {

            throw new ApiUserNotLoginErrorException("用户id不能为空");
        }//end if()

        $address_id = $request->address_id;
        if(empty($address_id)){

            throw new ApiInputErrorException("地址id不能为空");
        }//end if()


        try {

            $address_id = $addressService->removeAddress($address_id);

        } catch (AddressServiceException $e) {

            throw new ApiDoneErrorException($e->getMessage());
        }

        return response()->json(["result"=>"true"]);

    }//end func removeAddress()

    /**
     * @api {post} /address/update_address 修改用户收货地址
     * @apiName Address/update_address
     * @apiGroup Address
     * @apiDescription 修改用户收货地址.
     *
     * @apiParam {String} address_id  地址id.
     * @apiParam {Object} addressArr  要修改的地址字段数组，如：addressArr = array('consignee'=>'小明','mobile'=>'15136168236').
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //用户收货地址更新成功
     * {
     *   "result": "true"
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 403
     * //用户收货地址更新失败
     * {
     *   "message": "用户id不能为空"
     * }
     * @apiErrorExample Error-Response 02:
     * HTTP/1.1 400
     * //用户收货地址更新失败
     * {
     *   "message": "收货地址id不能为空"
     * }
     * @apiErrorExample Error-Response 03:
     * HTTP/1.1 400
     * //用户收货地址更新失败
     * {
     *   "message": "地址信息数组不能为空"
     * }
     * @apiErrorExample Error-Response 04:
     * HTTP/1.1 400
     * //用户收货地址更新失败
     * {
     *   "message": "要修改的收货地址id不能为空"
     * }
     * @apiErrorExample Error-Response 05:
     * HTTP/1.1 400
     * //用户收货地址更新失败
     * {
     *   "message": "要修改的收货地址字段数组不能为空"
     * }
     * @apiErrorExample Error-Response 06:
     * HTTP/1.1 400
     * //用户收货地址更新失败
     * {
     *   "message": "收货人参数不能为空"
     * }
     * @apiErrorExample Error-Response 07:
     * HTTP/1.1 400
     * //用户收货地址更新失败
     * {
     *   "message": "收货人身份证参数不能为空"
     * }
     * @apiErrorExample Error-Response 08:
     * HTTP/1.1 400
     * //用户收货地址更新失败
     * {
     *   "message": "收货地址省份参数不能为空"
     * }
     * @apiErrorExample Error-Response 09:
     * HTTP/1.1 400
     * //用户收货地址更新失败
     * {
     *   "message": "收货地址城市参数不能为空"
     * }
     * @apiErrorExample Error-Response 10:
     * HTTP/1.1 400
     * //用户收货地址更新失败
     * {
     *   "message": "收货地址区域参数不能为空"
     * }
     * @apiErrorExample Error-Response 11:
     * HTTP/1.1 400
     * //用户收货地址更新失败
     * {
     *   "message": "收货地址街道参数不能为空"
     * }
     * @apiErrorExample Error-Response 12:
     * HTTP/1.1 400
     * //用户收货地址更新失败
     * {
     *   "message": "收货人手机号码参数不能为空"
     * }
     */
    public function updateAddress(Request $request, AddressService $addressService)
    {

        $user_id = $request->apiUser->get()->id;
        if (empty($user_id)) {

            throw new ApiUserNotLoginErrorException("用户id不能为空");
        }//end if()

        $address_id = $request->address_id;
        if(empty($address_id)){

            throw new ApiInputErrorException("收货地址id不能为空");
        }//end if()

        $addressArr = $request->addressArr;
        if(empty($addressArr)){

            throw new ApiInputErrorException("地址信息数组不能为空");
        }//end if()

        try {

            $address_id = $addressService->updateAddress($address_id, $addressArr);

        } catch (AddressServiceException $e) {

            throw new ApiInputErrorException($e->getMessage());
        }

        return response()->json(["result"=>"true"]);

    }//end func updateAddress


    /**
     * @api {post} /address/get_user_default_address 获取用户默认地址
     * @apiName Address/get_user_default_address
     * @apiGroup Address
     * @apiDescription 获取用户默认地址.
     *
     * @apiSuccess {String[]} addressArr       默认地址信息数组.
     * @apiSuccess {Number} address_id       地址id.
     * @apiSuccess {Number} user_id       用户id.
     * @apiSuccess {String} id_card       用户证件号（一般是身份证号）.
     * @apiSuccess {String} consignee     收货人姓名.
     * @apiSuccess {String} country       国家.
     * @apiSuccess {String} position_ids  省市区id集.
     * @apiSuccess {String} province      省份（河南省，北京市）.
     * @apiSuccess {String} city          城市.
     * @apiSuccess {String} district      区.
     * @apiSuccess {String} address       详细地址.
     * @apiSuccess {String} mobile        电话号码.
     * @apiSuccess {Number} is_default    是否是默认地址，0：非默认地址；1：默认地址.
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * //用户收货地址更新成功
     * {
     *   "address_id": 13,
     *   "user_id": 59,
     *   "id_card": "411024198903052224",
     *   "consignee": "李安",
     *   "country": "",
     *   "position_ids": "110000,110100,110107",
     *   "province": "北京市",
     *   "city": "北京市",
     *   "district": "石景山区",
     *   "address": "天山路1号",
     *   "mobile": "17737132820",
     *   "is_default": 1
     * }
     * @apiErrorExample Error-Response 01:
     * HTTP/1.1 403
     * //用户收货地址更新失败
     * {
     *   "message": "用户id不能为空"
     * }
     */
    public function getUserDefaultAddress(Request $request, AddressService $addressService)
    {
        $user_id = $request->apiUser->get()->id;
        if (empty($user_id)) {

            throw new ApiUserNotLoginErrorException("用户id不能为空");
        }//end if()

        try {

            $address = $addressService->getUserDefaultAddress($user_id);

        } catch (AddressServiceException $e) {

            throw new ApiInputErrorException($e->getMessage());
        }

        return response()->json($address);

    }//end func getUserDefaultAddress



}
